|
1
|
|
|
import createReducerViaMap from '../dist/redux-reducer-map' |
|
2
|
|
|
import expect from 'expect.js' |
|
3
|
|
|
|
|
4
|
|
|
describe('Reducer created via Redux Reducer Map', () => { |
|
5
|
|
|
|
|
6
|
|
|
const EMPTY_STATE = {} |
|
7
|
|
|
|
|
8
|
|
|
it('returns the initial state if given none', () => { |
|
9
|
|
|
|
|
10
|
|
|
const initialState = { foo: 'bar' } |
|
11
|
|
|
const reducerMap = {} |
|
12
|
|
|
const action = { type: 'NOP' } |
|
13
|
|
|
const reducedState = createReducerViaMap(reducerMap, initialState)(undefined, action) |
|
14
|
|
|
|
|
15
|
|
|
expect(reducedState).to.eql(initialState) |
|
16
|
|
|
}) |
|
17
|
|
|
|
|
18
|
|
|
it('passes state and action to the appropriate function of the map', () => { |
|
19
|
|
|
|
|
20
|
|
|
const reducerMap = { |
|
21
|
|
|
USED: (state, action) => ({ reducer: 'USED', action: action, state: state }), |
|
22
|
|
|
UNUSED: (state, action) => ({ reducer: 'UNUSED'}) |
|
|
|
|
|
|
23
|
|
|
} |
|
24
|
|
|
const reduce = createReducerViaMap(reducerMap, EMPTY_STATE) |
|
25
|
|
|
const STATE_VALUE = 1500 |
|
26
|
|
|
const ACTION_VALUE = 2500 |
|
27
|
|
|
const state = { value: STATE_VALUE } |
|
28
|
|
|
const action = { type: 'USED', value: ACTION_VALUE } |
|
29
|
|
|
const reducedState = reduce(state, action) |
|
30
|
|
|
|
|
31
|
|
|
expect(reducedState.reducer).to.be('USED') |
|
32
|
|
|
expect(reducedState.state.value).to.be(STATE_VALUE) |
|
33
|
|
|
expect(reducedState.action.value).to.be(ACTION_VALUE) |
|
34
|
|
|
}) |
|
35
|
|
|
|
|
36
|
|
|
it('throws an error by default if action type is not found', () => { |
|
37
|
|
|
const UNKNOWN_ACTION_TYPE = 'UNKNOWN' |
|
38
|
|
|
const reducerMap = {} |
|
39
|
|
|
const reduce = createReducerViaMap(reducerMap, EMPTY_STATE) |
|
40
|
|
|
const state = { value: 'Some value' } |
|
41
|
|
|
const action = { type: UNKNOWN_ACTION_TYPE } |
|
42
|
|
|
|
|
43
|
|
|
const usingUnknownActionType = () => { |
|
44
|
|
|
reduce(state, action) |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
expect(usingUnknownActionType).to.throwException((e) => { |
|
48
|
|
|
expect(e.message).to.contain(UNKNOWN_ACTION_TYPE) |
|
49
|
|
|
expect(e.state).to.eql(state) |
|
50
|
|
|
expect(e.action).to.eql(action) |
|
51
|
|
|
}) |
|
52
|
|
|
}) |
|
53
|
|
|
|
|
54
|
|
|
it('allows overriding default error handling behaviour on unknown action types', () => { |
|
55
|
|
|
const UNKNOWN_ACTION_TYPE = 'UNKNOWN' |
|
56
|
|
|
const reducerMap = {} |
|
57
|
|
|
const customErrorHandler = (state, action) => ( |
|
58
|
|
|
{ |
|
59
|
|
|
state: state, |
|
60
|
|
|
action: action |
|
61
|
|
|
} |
|
62
|
|
|
) |
|
63
|
|
|
const reduce = createReducerViaMap(reducerMap, EMPTY_STATE, customErrorHandler) |
|
64
|
|
|
const state = { value: 42 } |
|
65
|
|
|
const action = { type: UNKNOWN_ACTION_TYPE, value: 666 } |
|
66
|
|
|
const reducedState = reduce(state, action) |
|
67
|
|
|
|
|
68
|
|
|
expect(reducedState.state).to.eql(state) |
|
69
|
|
|
expect(reducedState.action).to.eql(action) |
|
70
|
|
|
}) |
|
71
|
|
|
|
|
72
|
|
|
it('provides a convenience error handler for returning the state unchanged', () => { |
|
73
|
|
|
const UNKNOWN_ACTION_TYPE = 'UNKNOWN' |
|
74
|
|
|
const reducerMap = {} |
|
75
|
|
|
const reduce = createReducerViaMap(reducerMap, EMPTY_STATE, createReducerViaMap.justReturnState) |
|
76
|
|
|
|
|
77
|
|
|
const state = { value: 42 } |
|
78
|
|
|
const action = { type: UNKNOWN_ACTION_TYPE, value: 666 } |
|
79
|
|
|
const reducedState = reduce(state, action) |
|
80
|
|
|
|
|
81
|
|
|
expect(reducedState).to.eql(state) |
|
82
|
|
|
}) |
|
83
|
|
|
|
|
84
|
|
|
it("returns the initial state when given redux's init action", () => { |
|
85
|
|
|
const action = { type: '@@redux/INIT' } |
|
86
|
|
|
const reducerMap = {} |
|
87
|
|
|
const initialState = { value: 666 } |
|
88
|
|
|
const reduce = createReducerViaMap(reducerMap, initialState) |
|
89
|
|
|
const reducedState = reduce(EMPTY_STATE, action) |
|
90
|
|
|
|
|
91
|
|
|
expect(reducedState).to.eql(initialState) |
|
92
|
|
|
}) |
|
93
|
|
|
|
|
94
|
|
|
it("lets the user override the reducer for redux's init action", () => { |
|
95
|
|
|
const action = { type: '@@redux/INIT' } |
|
96
|
|
|
const reducerMap = { |
|
97
|
|
|
'@@redux/INIT': (state, action) => ({ originalState: state, action }) |
|
98
|
|
|
} |
|
99
|
|
|
const reduce = createReducerViaMap(reducerMap, EMPTY_STATE) |
|
100
|
|
|
const reducedState = reduce(EMPTY_STATE, action) |
|
101
|
|
|
|
|
102
|
|
|
expect(reducedState.action).to.eql(action) |
|
103
|
|
|
expect(reducedState.originalState).to.eql(EMPTY_STATE) |
|
104
|
|
|
}) |
|
105
|
|
|
}) |
|
106
|
|
|
|
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.